﻿using ExDUIR.NET.Declaration;
using ExDUIR.NET.Handle;
using System;

namespace ExDUIR.NET.Resource
{
    public class ExResource
    {
        private readonly ExResourceHandle _handle;
        public bool IsValid => _handle.IsInvalid;
        public IntPtr Handle => _handle.Handle;
        internal ExResource(ExResourceHandle handle)
        {
            _handle = handle;
        }

        internal ExResource(string lpwzFile)
        {
            _handle = new ExResourceHandle(ExAPI.Ex_ResLoadFromFile(lpwzFile));
        }

        internal ExResource(byte[] lpData, IntPtr dwDataLen)
        {
            _handle = new ExResourceHandle(ExAPI.Ex_ResLoadFromMemory(lpData, dwDataLen));
        }

        public bool GetFile(string lpwzPath, out IntPtr lpFile, out IntPtr dwFileLen)
        {
            return ExAPI.Ex_ResGetFile(_handle.Handle, lpwzPath, out lpFile, out dwFileLen);
        }

        public bool GetFileFromAtom(int atomPath, out IntPtr lpFile, out IntPtr dwFileLen)
        {
            return ExAPI.Ex_ResGetFileFromAtom(_handle.Handle, atomPath, out lpFile, out dwFileLen);
        }

        public void Dispose() => _handle.Dispose();
    }
}
